package com.tz.fruit.service.impl;

import com.tz.fruit.dao.IAddressDao;
import com.tz.fruit.entity.Address;
import com.tz.fruit.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by asus on 2017/7/20.
 */
@Service
@Transactional
public class AddressServiceImpl implements IAddressService{

    @Autowired
    private IAddressDao addressDao;

    @Override
    public void save(Address address) {
        addressDao.save(address);
    }

    @Override
    public void deleteById(Long id) {
        addressDao.deleteById(id);
    }

    @Override
    public List<Address> findAll(Long userId) {
        return addressDao.findAll(userId);
    }

    @Override
    public Address findById(Long id) {
        return addressDao.findById(id);
    }

    @Override
    public List<Address> findByUserId(Long userId) {
        return addressDao.findByUserId(userId);
    }

    @Override
    public void update(Address address) {
        addressDao.update(address);
    }

    @Override
    public void setDefault(Address address) {
        List<Address> list = this.findByUserId(Long.valueOf(address.getUser().getUserId()));
        for (Address a : list){
            a.setIsDefault("0");
        }
        address.setIsDefault("1");
    }
}
